package com.ccys.doctor.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ccys.common.vo.ResponseResultApp;
import com.ccys.db.cache.UserUtil;
import com.ccys.db.dto.doctor.patientNote.CreatePatientNoteDto;
import com.ccys.db.entity.TblBookingConsultNote;
import com.ccys.db.entity.TblDoctor;
import com.ccys.db.entity.TblMember;
import com.ccys.db.entity.TblMemberNote;
import com.ccys.db.mapper.TblBookingConsultNoteMapper;
import com.ccys.db.mapper.TblMemberMapper;
import com.ccys.db.mapper.TblMemberNoteMapper;
import com.ccys.db.page.AppPageDto;
import com.ccys.db.vo.doctor.patientNote.FindPagePatientNoteVo;
import com.ccys.db.vo.doctor.patientNote.GetConsultNoteForEditVo;
import com.ccys.db.vo.doctor.patientNote.PatientNotesMakeVo;
import com.ccys.doctor.service.PatientNoteService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: WhiteCoat
 * @description: 患者备忘录接口
 * @author: ZhangJingCan
 * @create: 2024-02-05 15:55
 **/

@Service
public class PatientNoteServiceImpl implements PatientNoteService {

    /**
     * 患者备忘录
     */
    @Resource
    private TblMemberNoteMapper tblMemberNoteMapper;

    @Resource
    private TblMemberMapper tblMemberMapper;

    @Resource
    private TblBookingConsultNoteMapper tblBookingConsultNoteMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResultApp<FindPagePatientNoteVo> createPatientNote(CreatePatientNoteDto dto) {
        if (ObjectUtil.isEmpty(dto.getMember_id())) {
            return ResponseResultApp.error("The member_id field is required.");
        }
        if (ObjectUtil.isEmpty(dto.getContent())) {
            return ResponseResultApp.error("The content field is required.");
        }
        //获取当前医生
        TblDoctor doctor = UserUtil.getCurLoginDoctor();
        //准备数据添加数据库
        TblMemberNote note = new TblMemberNote();
        note.setDoctorId(doctor.getId());
        note.setContent(dto.getContent());
        note.setMemberId(dto.getMember_id());
        tblMemberNoteMapper.insertSelective(note);

        //准备返回数据
        return ResponseResultApp.success(publicPage(0, 1000, dto.getMember_id()));
    }

    @Override
    public ResponseResultApp<FindPagePatientNoteVo> getPatientNote(String memberId, Integer pageIndex, Integer pageSize) {
        //校验
        if (ObjectUtil.isEmpty(memberId)) {
            return ResponseResultApp.error("The member_id field is required.");
        }
        if (ObjectUtil.isEmpty(pageIndex)) {
            return ResponseResultApp.error("The page_index field is required.");
        }
        if (ObjectUtil.isEmpty(pageSize)) {
            return ResponseResultApp.error("The page_size field is required.");
        }

        //准备返回数据
        return ResponseResultApp.success(publicPage(pageIndex, pageSize, memberId));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResultApp<FindPagePatientNoteVo> deletePatientNote(CreatePatientNoteDto dto) {
        if (ObjectUtil.isEmpty(dto.getId())) {
            return ResponseResultApp.error("The note patient could not be found.");
        }
        if (ObjectUtil.isEmpty(dto.getMember_id())) {
            return ResponseResultApp.error("The member_id field is required.");
        }
        Example memberExample = new Example(TblMember.class);
        memberExample.createCriteria()
                .andEqualTo("id", dto.getMember_id())
                .andEqualTo("status", 1)
                .andEqualTo("delFlag", 0);
        TblMember member = tblMemberMapper.selectOneByExample(memberExample);
        if (ObjectUtil.isEmpty(member)) {
            return ResponseResultApp.error("Member invalid");
        }
        Example noteExample = new Example(TblMemberNote.class);
        noteExample.createCriteria()
                .andEqualTo("id", dto.getId())
                .andEqualTo("delFlag", 0);
        TblMemberNote note = tblMemberNoteMapper.selectOneByExample(noteExample);
        if (ObjectUtil.isEmpty(note)) {
            return ResponseResultApp.error("The note patient could not be found.");
        }

        //开始修改
        note.setDelFlag(1);
        tblMemberNoteMapper.updateByPrimaryKeySelective(note);

        //开始准备返回数据
        return ResponseResultApp.success(publicPage(0, 1000, member.getId()));
    }

    @Override
    public ResponseResultApp<GetConsultNoteForEditVo> getConsultNoteForEdit(String id) {
        if (ObjectUtil.isEmpty(id)) {
            return ResponseResultApp.error("illegal parameter");
        }
        Example example = new Example(TblBookingConsultNote.class);
        example.createCriteria()
                .andEqualTo("id", id)
                .andEqualTo("delFlag", 0);
        TblBookingConsultNote note = tblBookingConsultNoteMapper.selectOneByExample(example);
        if (ObjectUtil.isEmpty(note)) {
            return ResponseResultApp.error("The data does not exist, please refresh and try again.");
        }
        //文件路径地址
        List<String> listFile = new ArrayList<>();
        if (ObjectUtil.isNotEmpty(note.getFileNames())) {
            String[] split = note.getFileNames().split("\\|");
            for (int i = 0; i < split.length; i++) {
                listFile.add("https://s3-ap-southeast-1.amazonaws.com/" + "prv.staging.whitecoat.global/" + "ConsultNotes/" + split[i]);
            }
        }
        //返回对象
        GetConsultNoteForEditVo vo = new GetConsultNoteForEditVo();
        vo.setId(note.getId());
        vo.setBookingId(note.getBookingId());
        vo.setMemberId(note.getMemId());
        vo.setName(note.getName());
        vo.setDescription(note.getDescription());
        vo.setFilenames(note.getFileNames() != null ? note.getFileNames() : "");
        vo.setListFile(listFile);
        return ResponseResultApp.success(vo);
    }

    /**
     * 公共查询分页
     *
     * @param pageIndex 开始页
     * @param pageSize  页数
     * @param memberId  用户id
     * @return
     */
    public FindPagePatientNoteVo publicPage(Integer pageIndex, Integer pageSize, String memberId) {
        //开启分页
        AppPageDto.startPage();
        List<PatientNotesMakeVo> list = tblMemberNoteMapper.findPageByMemberId(pageIndex, pageSize, memberId);
        PageInfo<PatientNotesMakeVo> pageInfo = new PageInfo<>(list);
        //返回对象
        FindPagePatientNoteVo vo = new FindPagePatientNoteVo();
        vo.setTotalPage(pageInfo.getPages());
        vo.setPatientNotes(list);
        return vo;
    }
}
